package com.game77.guessTheWords2.sprite;

import java.text.DecimalFormat;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;

import com.game77.guessTheWords2.MainActivity;
import com.game77.guessTheWords2.R;
import com.game77.guessTheWords2.core.GameSystem;
import com.game77.guessTheWords2.core.StageManager;
import com.game77.guessTheWords2.entity.StageData;
import com.game77.guessTheWords2.scene.ClassChooseScene;
import com.game77.guessTheWords2.scene.StageScene;
import com.game77.guessTheWords2.scene.ToolShopScene;
import com.game77.guessTheWords2.util.ADHelper;
import com.game77.guessTheWords2.util.AppControl;
import com.game77.guessTheWords2.util.AudioHelper;
import com.game77.guessTheWords2.util.GameUtil;
import com.umeng.analytics.MobclickAgent;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.types.WYColor3B;

public class ClassItemBlock extends Node {

	Button btnBackNormal;
	Button btnBackLock;
	Button btnBackUnable;
	Label labelTime;
	Label labelStar;
	Label labelIndex;

	int classIndex;

	public ClassItemBlock(int index) {
		classIndex = index;
		btnBackNormal = Button.make(R.drawable.classitem_normal,
				R.drawable.classitem_normal_press, this, "OnNormalBtnClick");
		btnBackNormal.setPosition(0, 0);
		this.addChild(btnBackNormal);

		btnBackLock = Button.make(R.drawable.classitem_lock,
				R.drawable.classitem_lock_press, this, "OnLockBtnClick");
		btnBackLock.setPosition(0, 0);
		btnBackLock.setVisible(false);
		this.addChild(btnBackLock);

		btnBackUnable = Button.make(R.drawable.classitem_unable,
				R.drawable.classitem_unable_press, this, "OnUnableBtnClick");
		btnBackUnable.setPosition(0, 0);
		btnBackUnable.setEnabled(false);
		btnBackUnable.setVisible(false);
		this.addChild(btnBackUnable);

		labelIndex = Label.make(classIndex + "", 33, Typeface.BOLD);
		labelIndex.setColor(WYColor3B.make(0, 0, 0));
		labelIndex.setPosition(0, 42);
		this.addChild(labelIndex);

		labelTime = Label.make("0'00", 23, Typeface.BOLD);
		labelTime.setColor(WYColor3B.make(255, 255, 255));
		labelTime.setAnchorX(0);
		labelTime.setPosition(-14, -10);
		this.addChild(labelTime);

		labelStar = Label.make("0/150", 18, Typeface.BOLD);
		labelStar.setColor(WYColor3B.make(255, 255, 255));
		labelStar.setAnchorX(0);
		labelStar.setPosition(-14, -43);
		this.addChild(labelStar);

		refreshClassItem();
	}

	public void refreshClassItem() {
		switch (StageManager.instance.getClassStatus(classIndex)) {
		case 1:
			btnBackNormal.setVisible(true);
			labelTime.setVisible(true);
			labelStar.setVisible(true);
			btnBackLock.setVisible(false);
			btnBackUnable.setVisible(false);
			setClassValues();
			break;
		case 2:
			btnBackNormal.setVisible(false);
			labelTime.setVisible(false);
			labelStar.setVisible(false);
			btnBackLock.setVisible(true);
			btnBackUnable.setVisible(false);
			break;
		case 3:
			btnBackNormal.setVisible(false);
			labelTime.setVisible(false);
			labelStar.setVisible(false);
			btnBackLock.setVisible(false);
			btnBackUnable.setVisible(true);
			break;
		}

	}

	private void setClassValues() {
		// 总的游戏时间
		int totalSeconds = 0;
		int totalStar = 0;
		for (int i = 1; i <= GameSystem.stageCount; i++) {
			String key = classIndex + "_" + i;
			if (StageManager.instance.getStageDatas().containsKey(key)) {
				StageData sd = StageManager.instance.getStageDatas().get(key);
				if (sd.getAllCount() > 0) {
					totalSeconds += sd.getTotalSeconds();
					totalStar += getStar(sd.getComplateCount()
							/ (float) sd.getAllCount());
				}
			}
		}
		labelStar.setText(totalStar + "/150");
		labelTime.setText(getUseTimeStr(totalSeconds));
	}

	public void OnNormalBtnClick() {
		// 设置当前章节
		AudioHelper.clickButton();
		StageManager.instance.setLastClass(classIndex);
		GameUtil.switchSceneNormalFast(StageScene.make());
		StageScene.make().refreshStages();
		ADHelper.hideAd();
	}

	static DecimalFormat secondFormat = new DecimalFormat("00");

	private String getUseTimeStr(long totalSeconds) {
		return (totalSeconds / 60) + "'"
				+ secondFormat.format(totalSeconds % 60);
	}

	public int getStar(float rate) {
		int result = 0;
		if (rate < 0.1f) {
			result = 0;
		} else if (rate < 0.4f) {
			result = 1;
		} else if (rate < 0.8f) {
			result = 2;
		} else {
			result = 3;
		}
		return result;

	}

	public synchronized void OnLockBtnClick() {
		// 购买章节
		int price = 60;
		if (StageManager.instance.isEnoughGold(price)) {
			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					MobclickAgent.onEvent(MainActivity.instance, "buyChapter");
				}
			});

			AudioHelper.buy();
			if (StageManager.instance.consumeGold(price)) {
				StageManager.instance.unlockClassStatus(classIndex);
				ClassChooseScene.make().refreshClassInfos();
				ClassChooseScene.make().updateCoins();
				StageManager.instance.unlockClassAllStage(classIndex);
			}
		} else {
			if (!AppControl.isSwitcherOn()) {
				MainActivity.instance.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						AlertDialog alertDialog = new AlertDialog.Builder(
								MainActivity.instance)
								.setTitle("提示")
								.setMessage("金币不足哦，是否到道具商店免费赚取金币？")
								.setPositiveButton("免费赚取",
										new DialogInterface.OnClickListener() {
											public void onClick(
													DialogInterface dialog,
													int which) {
												// 切换到道具商店

												GameUtil.switchSceneNormalFast(ToolShopScene
														.make());
												ToolShopScene.make().LastScene = 2;

											}
										})
								.setNegativeButton("取消",
										new DialogInterface.OnClickListener() {
											public void onClick(
													DialogInterface dialog,
													int which) {
												return;
											}
										}).setCancelable(false).create();
						alertDialog.show();
					}
				});
			}
		}
	}

	public void OnUnableBtnClick() {

	}

}
